Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
Nesting multiple [Op.not] queries in a single where clause

I've got a filter that allows users to indicate themes and genres they would like removed from the search results. This is an OR, not AND situation, so if either a theme or a genre is matched, it should be removed from the pool.

If I run the query below with a single [Op.not] clause, the themes and genres filter correctly. When I attempt to combine them as show in the example, only the genres [Op.not] executes. I assume I've chained them incorrectly in the where clause?

Code Example

    //Variables to store filters from JSON body
    let sortBy = [req.body.galleryFilters.sortBy, 'ASC']
    let hiddenThemes = [];
    let hiddenGenres = [];

    //Parse the body for activeFilters create and array of Active Filters 
    req.body.galleryFilters.themes.forEach(theme => {
      if(theme.hidden === true) {
        hiddenThemes.push(theme.name)
      }
    })

    req.body.galleryFilters.genres.forEach(genre => {
      if(genre.hidden === true) {
        hiddenGenres.push(genre.name)
      }
    })

    //use variables above to create new playlist obje
    const playlists = await db.playlists.findAll({
      where: {
        [Op.not]: {
          themes: {
            [Op.overlap]: hiddenThemes
          },
        },
        [Op.not]: {
          genres: {
            [Op.overlap]: hiddenGenres
          },
        },
      },
      order: [[req.body.galleryFilters.sortBy, 'ASC']]
    });
  
    return res.send(playlists);
  
  }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm too inexperienced to give a thorough explanation but the revision below seems to work for now:

 const playlists = await db.playlists.findAll({
      where: {
        [Op.not]: {
          [Op.or]: {
            themes: { [Op.overlap]: hiddenThemes },
            genres: { [Op.overlap]: hiddenGenres },
          }
        }
      },
      order: [[req.body.galleryFilters.sortBy, 'ASC']]
    });

    return res.send(playlists);

  }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda